﻿@model UserEditModel
@{
    ViewData["Title"] = "User: " + Model.User.Email;
}

@section Scripts {
    <script>
        (() => {
            document.getElementById('upgrade-premium').addEventListener('click', () => {
                if (document.getElementById('@(nameof(Model.Premium))').checked) {
                    alert('User is already premium.');
                    return;
                }

                // Premium
                document.getElementById('@(nameof(Model.MaxStorageGb))').value = '1';
                document.getElementById('@(nameof(Model.Premium))').checked = true;
                // Licensing
                document.getElementById('@(nameof(Model.LicenseKey))').value = '@Model.RandomLicenseKey';
                document.getElementById('@(nameof(Model.PremiumExpirationDate))').value =
                    '@Model.OneYearExpirationDate';
            });

            document.getElementById('gateway-customer-link').addEventListener('click', () => {
                const gateway = document.getElementById('@(nameof(Model.Gateway))');
                const customerId = document.getElementById('@(nameof(Model.GatewayCustomerId))');
                if (!gateway || gateway.value === '' || !customerId || customerId.value === '') {
                    return;
                }

                if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
                    window.open('https://dashboard.stripe.com/customers/' + customerId.value, '_blank');
                } else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
                    window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/'
                        + customerId.value, '_blank');
                }
            });

            document.getElementById('gateway-subscription-link').addEventListener('click', () => {
                const gateway = document.getElementById('@(nameof(Model.Gateway))');
                const subId = document.getElementById('@(nameof(Model.GatewaySubscriptionId))');
                if (!gateway || gateway.value === '' || !subId || subId.value === '') {
                    return;
                }

                if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
                    window.open('https://dashboard.stripe.com/subscriptions/' + subId.value, '_blank');
                } else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
                    window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/' +
                        'subscriptions/' + subId.value, '_blank');
                }
            });
        })();
    </script>
}

<h1>User <small>@Model.User.Email</small></h1>

<h2>Information</h2>
@Html.Partial("_ViewInformation", Model)
<form method="post" id="edit-form">
    <h2>General</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="Name"></label>
                <input type="text" class="form-control" asp-for="Name">
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="Email"></label>
                <input type="email" class="form-control" asp-for="Email" required>
            </div>
        </div>
    </div>
    <div class="form-check mb-3">
        <input type="checkbox" class="form-check-input" asp-for="EmailVerified">
        <label class="form-check-label" asp-for="EmailVerified"></label>
    </div>
    <h2>Premium</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="MaxStorageGb"></label>
                <input type="number" class="form-control" asp-for="MaxStorageGb" min="1">
            </div>
        </div>
    </div>
    <div class="form-check mb-3">
        <input type="checkbox" class="form-check-input" asp-for="Premium">
        <label class="form-check-label" asp-for="Premium"></label>
    </div>
    <h2>Licensing</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="LicenseKey"></label>
                <input type="text" class="form-control" asp-for="LicenseKey">
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="PremiumExpirationDate"></label>
                <input type="datetime-local" class="form-control" asp-for="PremiumExpirationDate">
            </div>
        </div>
    </div>
    <h2>Billing</h2>
    <div class="row">
        <div class="col-md">
            <div class="form-group">
                <div class="form-group">
                    <label asp-for="Gateway"></label>
                    <select class="form-control" asp-for="Gateway"
                            asp-items="Html.GetEnumSelectList<Bit.Core.Enums.GatewayType>()">
                        <option value="">--</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-md">
            <div class="form-group">
                <label asp-for="GatewayCustomerId"></label>
                <div class="input-group">
                    <input type="text" class="form-control" asp-for="GatewayCustomerId">
                    <div class="input-group-append">
                        <button class="btn btn-secondary" type="button" id="gateway-customer-link">
                            <i class="fa fa-external-link"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md">
            <div class="form-group">
                <label asp-for="GatewaySubscriptionId"></label>
                <div class="input-group">
                    <input type="text" class="form-control" asp-for="GatewaySubscriptionId">
                    <div class="input-group-append">
                        <button class="btn btn-secondary" type="button" id="gateway-subscription-link">
                            <i class="fa fa-external-link"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<div class="d-flex mt-4">
    <button type="submit" class="btn btn-primary" form="edit-form">Save</button>
    <div class="ml-auto d-flex">
        <button class="btn btn-secondary mr-2" type="button" id="upgrade-premium">
            Upgrade Premium
        </button>
        <form asp-action="Delete" asp-route-id="@Model.User.Id"
              onsubmit="return confirm('Are you sure you want to delete this user (@Model.User.Email)?')">
            <button class="btn btn-danger" type="submit">Delete</button>
        </form>
    </div>
</div>
